package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.OrderBean;
import top.drinyeit.weilong.model.TemporaryOrderDetailBean;

/**
 * Created by wu on 2018/1/15.
 */

public class ConfirmOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    public static final int TYPE_STORE = 0;
    public static final int TYPE_COMMODITY = 1;
    private List<Map> listNumber = new ArrayList<>();
    private List<TemporaryOrderDetailBean.OrderInfo.ChildrenOrder> list;

    public ConfirmOrderAdapter(List<TemporaryOrderDetailBean.OrderInfo.ChildrenOrder> list) {
        this.list = list;
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("type", TYPE_STORE);
            map.put("store_number", i);
            map.put("commodity_number", -1);
            listNumber.add(map);
            for (int j = 0; j < list.get(i).getCommodity_list().size(); j++) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("type", TYPE_COMMODITY);
                map1.put("store_number", i);
                map1.put("commodity_number", j);
                listNumber.add(map1);
            }
        }
    }

    public class StoreViewHolder extends RecyclerView.ViewHolder {
        public TextView store_name;
        public TextView order_state;

        public StoreViewHolder(View itemView) {
            super(itemView);
            store_name = itemView.findViewById(R.id.store_name);
            order_state = itemView.findViewById(R.id.order_state);
        }
    }

    public class CommodityViewHolder extends RecyclerView.ViewHolder {
        public ImageView m_img;
        public TextView commodity_name, weight, sale_price, commodity_sum;
        public LinearLayout btn;
        public CommodityViewHolder(View itemView) {
            super(itemView);
            m_img = itemView.findViewById(R.id.m_img);
            commodity_name = itemView.findViewById(R.id.commodity_name);
            commodity_sum = itemView.findViewById(R.id.commodity_sum);
            weight = itemView.findViewById(R.id.weight);
            sale_price = itemView.findViewById(R.id.sale_price);
            btn = itemView.findViewById(R.id.btn);

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        switch (viewType) {
            case TYPE_STORE:
                return new StoreViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_title, parent, false));
            case TYPE_COMMODITY:
                return new CommodityViewHolder(LayoutInflater.from(context).inflate(R.layout.item_lanzi_merchandise, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemType = (int) listNumber.get(position).get("type");
        int store_number = (int) listNumber.get(position).get("store_number");
        int commodity_number = (int) listNumber.get(position).get("commodity_number");
        switch (itemType) {
            case TYPE_STORE:
                ((StoreViewHolder) holder).store_name.setText(list.get(store_number).getStore_name());
                ((StoreViewHolder) holder).order_state.setVisibility(View.GONE);
                break;
            case TYPE_COMMODITY:
                ((CommodityViewHolder) holder).commodity_name.setText(list.get(store_number).getCommodity_list().get(commodity_number).getCommodity_name());
                ((CommodityViewHolder) holder).weight.setText(list.get(store_number).getCommodity_list().get(commodity_number).getWeight());
                ((CommodityViewHolder) holder).sale_price.setText("¥" + list.get(store_number).getCommodity_list().get(commodity_number).getSale_price());
                ((CommodityViewHolder) holder).commodity_sum.setText(list.get(store_number).getCommodity_list().get(commodity_number).getCommodity_sum());
                Glide.with(context).load(list.get(store_number).getCommodity_list().get(commodity_number).getCommodity_picture())
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.drawable.ic_def)
                        .dontTransform()
                        .into(((CommodityViewHolder) holder).m_img);
                ((CommodityViewHolder) holder).btn.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return (int) listNumber.get(position).get("type");
    }

    @Override
    public int getItemCount() {
        return listNumber.size();
    }

    public int getCoCount() {
        int j = 0;
        for (int i = 0; i < list.size(); i++) {
            j = j + list.get(i).getCommodity_list().size();
        }
        return j;
    }
}
